package com.asset.dispoal.file.judgment;

import com.asset.dispoal.file.judgment.api.IJudgmentBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.core.file.FileUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * @description:
 * @author: yf
 * @date: 2022-12-13 15:46:56
 */
@RestController
@Api(value = "判决书管理", tags = "判决书管理")
@RequestMapping("file/judgmentBook")
public class FileJudgmentBook {
    @Autowired
    private IJudgmentBookService judgmentBookService;
    @Autowired
    private Environment environment;

    @ApiOperation(value = "判决书压缩文件上传", notes = "判决书压缩文件上传")
    @PostMapping(value = "/upload")
    public Result<String> evidenceUpload(@ApiParam("总片数") @RequestParam(name = "dzTotalChunkCount", required = false) String chunks,
                                         @ApiParam("当前片索引") @RequestParam(name = "dzChunkIndex", required = false) String chunk,
                                         @RequestParam(name = "dzUuid", required = false) String taskId,
                                         @RequestParam(name = "dzFilename", required = false) String name,
                                         @RequestParam(name = "fileName", required = false) String filename,
                                         @RequestParam("file") MultipartFile file, @RequestParam("uid") String uuid) {
        return judgmentBookService.judgmentUpload(chunks, chunk, taskId, name, file, filename, uuid);
    }

    @ApiOperation(value = "根据地址下载判决书表格文件", notes = "根据地址下载判决书表格文件")
    @GetMapping("downloadJudgmentFile")
    public ResponseEntity<byte[]> downloadEvidenceFile(String fileUrl) {
        String evidenceUrl = environment.getProperty("file.evidence.url");
        String filePath = Paths.get(evidenceUrl, fileUrl).toString();
        File file = new File(filePath);
        return commonDownload(file);
    }


    private ResponseEntity<byte[]> commonDownload(File file) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", new String(file.getName().getBytes(), "iso-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(FileUtil.readFileToByteArray(file), headers, HttpStatus.CREATED);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
